<?php
/**
 * @author	1054229380@qq.com
 * @since	2017-7-3
 * @desc session
 **/
 
function mysession_open($save_path, $session_name) {
	//global $myPDO;
	return true;
}

function mysession_close() {
	return true;
}

function mysession_read($key) {
	$expiry_time = time();
	$_tempInfo=db::getInfo(array(
			"tableName"=>"sys_session",
			"selectField"=>"session_data",
			"whereCondition"=>"where session_key = '$key' and session_expiry > $expiry_time"
	));
	$session_data=isset($_tempInfo['session_data']) ? $_tempInfo['session_data']:"";
	return $session_data;
}

function mysession_write($key, $data) {

	global $DbConfig,$myPDO;
	//var_dump($myPDO);
	//exit();
	$tmpMyPDO=new myPDO($DbConfig);
	$gctime = ini_get('session.gc_maxlifetime');
	$expiry_time = time() + $gctime;
	$_sessInfo=$tmpMyPDO->getAll("select session_data from sys_session where session_key = '$key' limit 0,1");
	if(isset($_sessInfo[0]['session_data'])){
		$sql="update sys_session set session_data = '$data', session_expiry = $expiry_time where session_key = '$key'";
	}else{
		$sql="insert into sys_session(session_key,session_data,session_expiry) values('$key', '$data', $expiry_time)";
	}
	//echo $sql;
	if($data!=""){
		$tmpMyPDO->query($sql);
	}
	$tmpMyPDO=null;
}

function mysession_destroy($key) {
	$sql="delete from sys_session where session_key = '$key'";
	db::runsql($sql);
}

function mysession_gc($expiry_time) {
	$expiry_time = time();
	$sql="delete from sys_session where session_expiry < $expiry_time";
	db::runsql($sql);
}

ini_set('session.use_trans_sid', 0);
ini_set('session.gc_maxlifetime', 36000); //生存周期X秒
ini_set("session.gc_probability", "100"); //99%的概率清空
ini_set("session.gc_divisor", "100");
ini_set('session.save_handler', 'user');
session_set_save_handler('mysession_open', 'mysession_close', 'mysession_read', 'mysession_write', 'mysession_destroy', 'mysession_gc');
?>